<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Users as UsersModel;

class UsersService
{
    /**
     * 本月数据(折线图)
     * @return array
     */
    public static function getUsersMonthLine()
    {
        $startEndTime = getStartEndTime();
        $usersMonth = UsersModel::getUsersMonth($startEndTime['startTime'], $startEndTime['endTime']);
        return inArr($usersMonth, $startEndTime['dateArr']);
    }

    /**
     * 本周数据(折线图)
     */
    public static function getUsersWeekLine()
    {
        $usersWeek = UsersModel::getUsersWeek();
        return weekArr($usersWeek);
    }

    /**
     * 本周新增
     * @return int
     */
    public static function countUsersWeekCreate()
    {
        return UsersModel::countUsersWeekCreate();
    }

    /**
     * 用户总数
     * @param array $where
     * @return int
     */
    public static function countUsers($where = [])
    {
        return UsersModel::countUsers($where);
    }

    /**
     * 昨日新增用户
     * @param array $where
     * @return int
     */
    public static function yesTodayUsers($where = [])
    {
        return UsersModel::yesTodayUsers($where);
    }

    /**
     * 实时新增用户
     * @param array $where
     * @return int
     */
    public static function todayUsers($where = [])
    {
        return UsersModel::todayUsers($where);
    }

    /**
     * 用户列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getUsersList($where, $page, $pagesize)
    {
        $data = UsersModel::getUsersList($where, $page, $pagesize);

        foreach ($data as $k => $v) {
            $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div>';

            // 计算会员剩余天数
            $last_day = 0;
            $end_time = '-';
            if ($v['end_time']) {
                $last_time = $v['end_time'] - time();
                $last_day = round($last_time / 86400);
                $end_time = date('Y-m-d', $v['end_time']);
            }
            $data[$k]['nickname_data'] = '<div class="d-fix1 mar-lefts">' . $headimg . '<div><span class="datas">' . emojiDecode($v['nickname']) . '</span></div></div>';


            $map = ['uid' => $v['id']];
            // 社群数
            $resourceFields = ResourceService::resourceFields($map);
            $resource = $resourceFields ? join(' / ', $resourceFields) : '-';
            $data[$k]['ids'] = $v['id'] . '<br><span class="color3">' . $resource . '</span>';

            $iname = $v['iname'] ?: '游客';
            $auth_city = $v['auth_city'] ?: '未知';
            // 1公众号  2小程序
            $associate = $v['associate'] == 1 ? '<span class="color1">公众号</span>' : '<span class="color2">小程序</span>';
            $data[$k]['identity_data'] = $iname . '<br><span class="color3">' . $auth_city . '</span>';

            // 首次访问时间
            $first_time = UsersAccessLogService::findUsersAccessTime($map);
            $first_time = $first_time ? date('Y-m-d', $first_time) : '-';
            // 访问次数
            $access_nums = UsersAccessLogService::countUsersAccess($map);
            // 分享次数
            $share_nums = 0;
            $role = getRoleName($v['role']);
            $data[$k]['user_type'] = $role . '<br>' . $associate;
            $create_time = $v['create_time'] ? date('Y-m-d', strtotime($v['create_time'])) : '-';
            $data[$k]['date'] = $create_time . '<br>' . '<span class="color4">' . $end_time . '</span>';
            $data[$k]['access_data'] = $access_nums . '<br><span class="color3">' . $share_nums . '</span>';
        }

        return $data;
    }

    /**
     * 用户列表统计
     * @param $where
     * @return mixed
     */
    public static function countUsersList($where)
    {
        return UsersModel::countUsersList($where);
    }

    /**
     * 用户数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findUsers($where)
    {
        return UsersModel::findUsers($where);
    }

    /**
     * 更新用户信息
     * @param $data
     * @param $where
     * @return UsersModel
     */
    public static function updateUsers($data, $where)
    {
        return UsersModel::updateUsers($data, $where);
    }

    /**
     * 用户指定字段值
     * @param $where
     * @param string $field
     * @return mixed
     */
    public static function findUsersValue($where, $field = 'pub_openid')
    {
        return UsersModel::findUsersValue($where, $field);
    }

    /**
     * 用户地区分布
     * @return array
     */
    public static function getUsersArea()
    {
        $data = UsersModel::getUsersArea();
        $datas = getAreaData($data);
        return ['count' => count($data), 'datas' => $datas];
    }

    /**
     * 身份数据
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUsersIdentity()
    {
        $data = UsersCateService::getUsersIdentity();
        $peopleList = [];
        foreach ($data as $value) {
            $peopleList[$value['name']] = $value['nums'];
        }

        return getIdentityData($peopleList, 0);
    }

}